<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="test"></div>
    <div id="dome1"></div>
    <div id="dome2"></div>
    <div id="dome3"></div>
     <!-- 1引入核心 -->
     <script type="text/javascript" src="./js/react.development.js"></script>
     <!-- 2扩展库-->
     <script type="text/javascript" src="./js/react-dom.development.js"></script>
     <!-- 3引入babel，用于将jsx转为js-->
     <script type="text/javascript" src="./js/babel.min.js"></script>
    <!-- 用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="./js/prop-types.js"></script>


    <!-- 一、DOM的diffing算法-key值问题 -->
    <script type="text/babel">
        class Person extends React.Component{
            state = {
                persons:[
                    {id:1,name:'小红',age:15},
                    {id:2,name:'小吧',age:11}
                 ]
            }

           add =()=>{
                const {persons} = this.state
                const p =  {id:3,name:'小王',age:1}
                this.setState({persons:[p,...persons]})
           }

            render(){
                return(
                    <div>
                        <h2>使用index作为唯一标识</h2>
                        <button onClick={this.add}>添加一个小王</button>
                        <ul>
                            {
                                this.state.persons.map((personObj,index)=>{
                                    return <li key = {index}>{personObj.name}---{personObj.age}</li>
                                })
                            }    
                        </ul>
                        <hr/>
                        <h2>使用id作为唯一标识</h2>
                        <ul>
                            {
                                this.state.persons.map((personObj,index)=>{
                                    return <li key = {personObj.id}>{personObj.name}---{personObj.age}</li>
                                })
                            }    
                        </ul>
                     </div>
                   
                )
            }
        }

        
        ReactDOM.render(<Person/>,document.getElementById('test'))
    </script>




</body>
</html>